<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_question_class extends AWS_CONTROLLER
{

	function get_permission_action()
	{
	
	}

	public function setup()
	{
		admin_session_class::check_admin_login($this->get_permission_action());
		
		$this->crumb("问题管理", "admin/question/");
	}

	public function index_action()
	{
		$this->list_action();
	}

	public function question_list_action()
	{
		if ($_POST['keyword'])
		{
			header('Location: ' . $_SERVER['REQUEST_URI'] . (preg_match('/\/$/', $_SERVER['REQUEST_URI']) ? '' : '__') . 'keyword-' . $_POST['keyword']);
		}
		
		$per_page = 10;
		
		$page_id = intval($_GET['page']);
		
		$page_id = ($page_id == 0) ? 1 : $page_id;
		
		$limit = ($page_id - 1) * $per_page . "," . $per_page;
		
		$keyword = $_GET['keyword'];
		
		$page_url = get_setting('base_url') . '/' . G_INDEX_SCRIPT . "/admin/question/question_list/";
		
		$m_keyword = AWS_APP::model()->quote($keyword);
		
		if ($keyword)
		{
			$question_list = $this->model('question')->get_question_list(false, $m_keyword, $limit);
			$keyword_url .= "keyword-" . $keyword;
			$totalnum = $this->model('question')->get_question_list(true, $m_keyword);
		}
		else
		{
			$question_list = $this->model('question')->get_question_list(false, "", $limit);
			$totalnum = $this->model('question')->get_question_list(true, "");
		}
		
		if ($question_list)
		{
			foreach ($question_list as $key => $val)
			{
				$detail_tmp = FORMAT::format_content($val['question_detail']);
				
				$question_list[$key]['published_user_name'] = $this->model('account')->get_real_name_by_uid($val['published_uid']);
				
				if (empty($detail_tmp['content_title']))
				{
					$question_list[$key]['question_detail'] = $detail_tmp['content_content'];
				}
				else
				{
					$question_list[$key]['question_detail'] = $detail_tmp['content_title'];
				}
			}
		}
		
		$this->model('pagination')->initialize(array(
			'base_url' => $page_url . $keyword_url, 
			'total_rows' => $totalnum, 
			'per_page' => $per_page, 
			'last_link' => "末页", 
			'first_link' => "首页", 
			'next_link' => "下一页 »", 
			'prev_link' => "« 上一页", 
			'anchor_class' => ' class="number"', 
			'cur_tag_open' => '<a class="number current">', 
			'cur_tag_close' => '</a>', 
			'direct_page' => TRUE
		));
		
		TPL::assign('pagination', $this->model('pagination')->create_links());
		TPL::assign('keyword', $keyword);
		TPL::assign('list', $question_list);
		TPL::output("admin/question/question_list");
	}

	public function answer_list_action()
	{
		$question_id = $_GET['question_id'];
		
		if (empty($question_id))
		{
			H::redirect_msg('问题编号错误，请返回');
		}
		
		$per_page = 20;
		
		$page_id = intval($_GET['page']);
		
		$page_id = ($page_id == 0) ? 1 : $page_id;
		
		$limit = ($page_id - 1) * $per_page . "," . $per_page;
		
		$keyword = $_GET['keyword'];
		
		$page_url = get_setting('base_url') . '/' . G_INDEX_SCRIPT . "/admin/question/answer_list/question_id-" . $question_id;
		
		$question_info = $this->model("question")->get_question_info_by_id($question_id);
		
		if (empty($question_info))
		{
			H::redirect_msg('问题编号错误，请返回');
		}
		
		$answer_list = $this->model('answer')->get_answer_list_by_question_id($question_id, $limit);
		
		$this->model('pagination')->initialize(array(
			'base_url' => $page_url, 
			'total_rows' => $question_info['answer_count'], 
			'per_page' => $per_page, 
			'last_link' => "末页", 
			'first_link' => "首页", 
			'next_link' => "下一页 »", 
			'prev_link' => "« 上一页", 
			'anchor_class' => ' class="number"', 
			'cur_tag_open' => '<a class="number current">', 
			'cur_tag_close' => '</a>', 
			'direct_page' => TRUE
		));
		
		$this->crumb("问题 : {$question_info['question_content']}  回复列表", "?c=question&act=answer_list");
		
		TPL::assign('pagination', $this->model('pagination')->create_links());
		TPL::assign('list', $answer_list);
		TPL::output('admin/question/answer_list');
	}

	public function question_batch_action()
	{
		define('IN_AJAX', TRUE);
		
		$question_ids = $_POST['question_ids'];
		
		if (!$_POST['batch_action'])
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "请先选择操作"));
		}
		
		if (empty($question_ids))
		{
			H::ajax_json_output(AWS_APP::RSM(null, "-1", "请先选择问题"));
		}
		
		$q_ids = array();
		
		if (is_array($question_ids))
		{
			foreach ($question_ids as $key => $val)
			{
				$q_ids[] = $val;
			}
		}
		
		switch($_POST['batch_action'])
		{
			case 'delete' : 
				$retval = $this->model('question')->remove_question_by_ids($q_ids);
				break;
			default:
		}
		
		if($retval)
		{
			H::ajax_json_output(AWS_APP::RSM(array(), 1, "操作成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(), -1, "操作失败"));
		}
	}

	public function answer_batch_action()
	{
		define('IN_AJAX', TRUE);
	
		$answer_ids = $_POST['answer_ids'];
	
		if (!$_POST['batch_action'])
		{
			H::ajax_json_output(AWS_APP::RSM(array(), "-1", "请先选择操作"));
		}
	
		if (empty($answer_ids))
		{
			H::ajax_json_output(AWS_APP::RSM(array(), "-1", "请先选择问题"));
		}
	
		$a_ids = array();
	
		if (is_array($answer_ids))
		{
			foreach ($answer_ids as $key => $val)
			{
				$a_ids[] = $val;
			}
		}
		
		switch($_POST['batch_action'])
		{
			case 'delete' : 
				$retval = $this->model('answer')->remove_answers_by_ids($a_ids);
				break;
			default:
		}
		
		if($retval)
		{
			H::ajax_json_output(AWS_APP::RSM(array(), 1, "操作成功"));
		}
		else
		{
			H::ajax_json_output(AWS_APP::RSM(array(), -1, "操作失败"));
		}
	}
}